package com.example.springsecurity.controller;

import com.example.springsecurity.aop.SystemControllerLog;
import com.example.springsecurity.domain.SysUserOnline;
import com.example.springsecurity.security.User;
import com.example.springsecurity.service.ISysUserOnlineService;
import com.example.springsecurity.utils.ResultBean;
import com.example.springsecurity.utils.ifnull.StringUtils;
import com.example.springsecurity.utils.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName OnlineUserController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-08-26 11：18
 **/
@Controller
@RequestMapping("/online")
public class OnlineUserController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysUserOnlineService userOnlineService;

    @GetMapping("/user")
    @ResponseBody
    @PreAuthorize("@ss.hasRole('admin')")
    public List<SysUserOnline> getAllOnlineUser(String ipaddr, String userName){
        Collection<String> keys = redisCache.keys("login_tokens:" + "*");
        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
        for (String key : keys)
        {
            User user = redisCache.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName))
            {
                if (StringUtils.equals(ipaddr, user.getIp()) && StringUtils.equals(userName, user.getUsername()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                }
            }
            else if (StringUtils.isNotEmpty(ipaddr))
            {
                if (StringUtils.equals(ipaddr, user.getIp()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                }
            }
            else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getSysUser()))
            {
                if (StringUtils.equals(userName, user.getUsername()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                }
            }
            else
            {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return userOnlineList;
    }


    /**
     * 强退用户
     */
    @SystemControllerLog(description = "强退用户")
    @DeleteMapping("/{tokenId}")
    public ResultBean forceLogout(@PathVariable String tokenId)
    {
        redisCache.deleteObject("login_tokens:" + tokenId);
        return ResultBean.success("退出用户成功");
    }
}
